package com.ruoyi.carrepair.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.carrepair.domain.*;
import com.ruoyi.carrepair.mapper.QxMxPeijianMapper;
import com.ruoyi.carrepair.mapper.QxMxWeixiuMapper;
import com.ruoyi.carrepair.mapper.QxOperatelogMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.zjc.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.carrepair.mapper.QxWxdNrMapper;
import com.ruoyi.carrepair.service.IQxWxdNrService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 维修单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class QxWxdNrServiceImpl implements IQxWxdNrService {
    @Autowired
    private QxWxdNrMapper qxWxdNrMapper;

    @Autowired
    private QxMxPeijianMapper qxMxPeijianMapper;
    @Autowired
    private QxMxWeixiuMapper qxMxWeixiuMapper;
    @Autowired
    private QxOperatelogMapper qxOperatelogMapper;

    /**
     * 查询维修单管理
     *
     * @param id 维修单管理主键
     * @return 维修单管理
     */
    @Override
    public QxWxdNr selectQxWxdNrById(String id) {
        return qxWxdNrMapper.selectQxWxdNrById(id);
    }

    /**
     * 查询维修单管理列表
     *
     * @param qxWxdNr 维修单管理
     * @return 维修单管理
     */
    @Override
    public List<QxWxdNr> selectQxWxdNrList(QxWxdNr qxWxdNr) {
        qxWxdNr.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        return qxWxdNrMapper.selectQxWxdNrList(qxWxdNr);
    }

    @Override
    public QxWxdNrSummary selectSumQxWxdNr(QxWxdNr qxWxdNr) {
        qxWxdNr.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        return qxWxdNrMapper.selectSumQxWxdNr(qxWxdNr);
    }

    /**
     * 新增维修单管理
     *
     * @param qxWxdNr 维修单管理
     * @return 结果
     */
    @Override
    @Transactional
    public String insertQxWxdNr(QxWxdNr qxWxdNr, List<QxMxPeijian> listPeijian, List<QxMxWeixiu> listWeixiu,String danhao) {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();
        Date currentTime = new Date();
        //初始化维修单数据并创建
        qxWxdNr.setToApplet(toapplet);
        qxWxdNr.setCreateID(createUserId);
        qxWxdNr.setCreator(createUserName);
        qxWxdNr.setCreateDate(currentTime);
        if(qxWxdNr.getRiqi() == null){
            qxWxdNr.setRiqi(currentTime);
        }
        qxWxdNr.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxWxdNr.setDanhao(danhao);
        if(qxWxdNr.getJiesuanfangshi()==null){
            qxWxdNr.setJiesuanfangshi(1L);
        }
        qxWxdNr.setIsDeleted(0L);
        qxWxdNr.setRowVersion(0L);
        qxWxdNr.setIsjiesuan(1L);
        qxWxdNrMapper.insertQxWxdNr(qxWxdNr);
        //初始化设置配件明细并创建
        for(QxMxPeijian qxMxPeijian:listPeijian){
            qxMxPeijian.setNrId(qxWxdNr.getId());
            qxMxPeijian.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
            qxMxPeijian.setCreateID(createUserId);
            qxMxPeijian.setCreator(createUserName);
            qxMxPeijian.setCreateDate(new Date());
            qxMxPeijian.setToApplet(toapplet);
            if(qxMxPeijian.getNotselectdata() == null){
                qxMxPeijian.setNotselectdata(0L);
            }
            qxMxPeijianMapper.insertQxMxPeijian(qxMxPeijian);
        }
        //初始化配件明细
        for(QxMxWeixiu qxMxWeixiu:listWeixiu){
            qxMxWeixiu.setNrId(qxWxdNr.getId());
            qxMxWeixiu.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
            qxMxWeixiu.setCreateID(createUserId);
            qxMxWeixiu.setCreator(createUserName);
            qxMxWeixiu.setCreateDate(new Date());
            qxMxWeixiu.setToApplet(toapplet);
            if(qxMxWeixiu.getNotselectdata() == null){
                qxMxWeixiu.setNotselectdata(0L);
            }
            qxMxWeixiuMapper.insertQxMxWeixiu(qxMxWeixiu);
        }
        //添加日志记录
        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "新增了一条维修单信息。单号为：【" + qxWxdNr.getDanhao() + "】");
        qxOperatelog.setCategory("1");
        qxOperatelog.setMenuModel("维修单管理");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return qxWxdNr.getId();

    }

    /**
     * 修改维修单管理
     *
     * @param qxWxdNr 维修单管理
     * @return 结果
     */
    @Override
    @Transactional
    public String updateQxWxdNr(QxWxdNr qxWxdNr, List<QxMxPeijian> listPeijian, List<QxMxWeixiu> listWeixiu) {
        String toApplet = SecurityUtils.getLoginUser().getToApplet();
        Long createID = SecurityUtils.getLoginUser().getUserId();
        String creator = SecurityUtils.getLoginUser().getUsername();
        //第一步：删除配件明细表
        qxMxPeijianMapper.deleteQxMxPeijianBynrId(qxWxdNr.getId());
        //第二步：删除维修明细表
        qxMxWeixiuMapper.deleteQxMxWeixiuBynrId(qxWxdNr.getId());
        //第三步：修改维修单
        qxWxdNrMapper.updateQxWxdNr(qxWxdNr);
        //第四步：重新添加配件明细表
        for (QxMxPeijian qxMxPeijian : listPeijian) {
            qxMxPeijian.setNrId(qxWxdNr.getId());
            qxMxPeijian.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
            qxMxPeijian.setCreateID(createID);
            qxMxPeijian.setCreator(creator);
            qxMxPeijian.setCreateDate(new Date());
            qxMxPeijian.setToApplet(toApplet);
            if(qxMxPeijian.getNotselectdata() == null){
                qxMxPeijian.setNotselectdata(0L);
            }
            qxMxPeijianMapper.insertQxMxPeijian(qxMxPeijian);
        }
        //第五步：重新添加维修明细表
        for (QxMxWeixiu qxMxWeixiu : listWeixiu) {
            qxMxWeixiu.setNrId(qxWxdNr.getId());
            qxMxWeixiu.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
            qxMxWeixiu.setCreateID(createID);
            qxMxWeixiu.setCreator(creator);
            qxMxWeixiu.setCreateDate(new Date());
            qxMxWeixiu.setToApplet(toApplet);
            if(qxMxWeixiu.getNotselectdata() == null){
                qxMxWeixiu.setNotselectdata(0L);
            }
            qxMxWeixiuMapper.insertQxMxWeixiu(qxMxWeixiu);
        }
        //第六步：添加日志记录
        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(creator + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "更新了一条维修单信息。单号为：【" + qxWxdNr.getDanhao() + "】");
        qxOperatelog.setCategory("2");
        qxOperatelog.setMenuModel("维修单管理");
        qxOperatelog.setCreateID(createID);
        qxOperatelog.setCreator(creator);
        qxOperatelog.setToApplet(toApplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);
        return qxWxdNr.getId();
    }

    /**
     * 批量删除维修单管理
     *
     * @param ids 需要删除的维修单管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteQxWxdNrByIds(String[] ids) {
        String idsStr="";
        for(String id:ids){
            idsStr = idsStr + id + ",";
        }
        String toApplet = SecurityUtils.getLoginUser().getToApplet();
        Long createID = SecurityUtils.getLoginUser().getUserId();
        String creator = SecurityUtils.getLoginUser().getUsername();
        int num = qxWxdNrMapper.deleteUpdQxWxdNrByIds(ids);
        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(creator + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "删除了维修单信息。维修单id：【" + idsStr+ "】");
        qxOperatelog.setCategory("3");
        qxOperatelog.setMenuModel("维修单管理");
        qxOperatelog.setCreateID(createID);
        qxOperatelog.setCreator(creator);
        qxOperatelog.setToApplet(toApplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);
        return num;
    }

    /**
     * 删除维修单管理信息
     *
     * @param id 维修单管理主键
     * @return 结果
     */
    @Override
    public int deleteQxWxdNrById(String id) {
        return qxWxdNrMapper.deleteQxWxdNrById(id);
    }

    @Override
    public int selectNums(String riqi) {
        return qxWxdNrMapper.selectNums(riqi);
    }

    @Override
    @Transactional
    public int auditById(String[] ids) {
        String idsStr="";
        for(String id:ids){
            idsStr = idsStr + id + ",";
        }
        String toApplet = SecurityUtils.getLoginUser().getToApplet();
        Long createID = SecurityUtils.getLoginUser().getUserId();
        String creator = SecurityUtils.getLoginUser().getUsername();

        for(String id:ids){
            qxWxdNrMapper.auditById(id);
        }

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(creator + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "审核了维修单信息。维修单id：【" + idsStr+ "】");
        qxOperatelog.setCategory("4");
        qxOperatelog.setMenuModel("维修单管理");
        qxOperatelog.setCreateID(createID);
        qxOperatelog.setCreator(creator);
        qxOperatelog.setToApplet(toApplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);
        return 1;
    }
}
